package tw.twandroidproj.app.ui.fragment;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;


import java.util.ArrayList;

import tw.twandroidproj.app.R;

/**
 * Created by Raymon on 2016/10/13.
*/
public class Header_fragment extends Fragment{
    private LinearLayout ll_focus_indicator_container = null;
    private MyGallery gallery = null;
    private ArrayList<Drawable> imgList = new ArrayList<Drawable>();
    /**
     * 存储上一个选择项的Index
     */
    private int preSelImgIndex = 0;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View layout = inflater.inflate(R.layout.fragment_main_head, container, false);
        InitImgList();
        ll_focus_indicator_container = (LinearLayout) layout.findViewById(R.id.ll_focus_indicator_container);
        InitFocusIndicatorContainer();
        gallery = (MyGallery) layout.findViewById(R.id.banner_gallery);
        gallery.setAdapter(new ImageAdapter(getActivity()));
        gallery.setFocusable(true);
        gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> arg0, View arg1,
                                       int selIndex, long arg3) {
                //修改上一次选中项的背景
                selIndex = selIndex % imgList.size();

                ImageView preSelImg = (ImageView) ll_focus_indicator_container
                        .findViewById(preSelImgIndex);
                preSelImg.setImageDrawable(Header_fragment.this
                        .getResources().getDrawable(R.drawable.ic_focus));
                //修改当前选中项的背景
                ImageView curSelImg = (ImageView) ll_focus_indicator_container
                        .findViewById(selIndex);
                curSelImg
                        .setImageDrawable(Header_fragment.this
                                .getResources().getDrawable(
                                        R.drawable.ic_focus_select));
                preSelImgIndex = selIndex;
            }

            public void onNothingSelected(AdapterView<?> arg0) {
            }
        });
        Log.i("msg", "260px:" + px2dip(getActivity(), 260));


        return layout;
    }

    public static float dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (dpValue * scale + 0.5f);
    }

    public static float px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        Log.i("msg", "scale:" + scale);
        return (pxValue / scale + 0.5f);
    }


    private void InitFocusIndicatorContainer() {
        for (int i = 0; i < imgList.size(); i++) {
            ImageView localImageView = new ImageView(getActivity());
            localImageView.setId(i);
            ImageView.ScaleType localScaleType = ImageView.ScaleType.FIT_XY;
            localImageView.setScaleType(localScaleType);
            LinearLayout.LayoutParams localLayoutParams = new LinearLayout.LayoutParams(
                    24, 24);
            localImageView.setLayoutParams(localLayoutParams);
            localImageView.setPadding(5, 5, 5, 5);
            localImageView.setImageResource(R.drawable.ic_focus);
            this.ll_focus_indicator_container.addView(localImageView);
        }
    }

    private void InitImgList() {
        // 加载图片数据（本demo仅获取本地资源，实际应用中，可异步加载网络数据）
        imgList.add(this.getResources().getDrawable(R.drawable.a1));
        imgList.add(this.getResources().getDrawable(R.drawable.a2));
        imgList.add(this.getResources().getDrawable(R.drawable.a3));
        imgList.add(this.getResources().getDrawable(R.drawable.a4));

    }

    public class ImageAdapter extends BaseAdapter {
        private Context _context;

        public ImageAdapter(Context context) {
            _context = context;
        }

        public int getCount() {
            return Integer.MAX_VALUE;
        }

        public Object getItem(int position) {

            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                ImageView imageView = new ImageView(_context);
                imageView.setAdjustViewBounds(true);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                imageView.setLayoutParams(new Gallery.LayoutParams(
                        Gallery.LayoutParams.WRAP_CONTENT, Gallery.LayoutParams.WRAP_CONTENT));
                convertView = imageView;
                viewHolder.imageView = (ImageView) convertView;
                convertView.setTag(viewHolder);

            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.imageView.setImageDrawable(imgList.get(position % imgList.size()));

            return convertView;
        }

    }

    private static class ViewHolder {
        ImageView imageView;
    }

    public void onDestroy() {
        super.onDestroy();
//        gallery.destroy();
    }
}